#include <stdio.h>
#include "RBTree.h"

void recursion_traverse(RBTreeNode *root)
{
    if (root == NIL)
    {
        return;
    }

    if (root != NIL)
    {
        printf("key: %d,parent: %d,position: %d,color: %d\n",
               *(GLOBAL_TYPE *)root->pkey,
               *(GLOBAL_TYPE *)root->pParentNode->pkey,
               (*(GLOBAL_TYPE *)root->pParentNode->pLChild->pkey == *(GLOBAL_TYPE *)root->pkey) ? 0 : 1,
               root->color);
    }

    recursion_traverse(root->pLChild);
    recursion_traverse(root->pRChild);
}

int main()
{
    rbtree_initNIL();
    RBTreeNode *rbtree = NIL;

    while (1)
    {
        int purpose = -1;
        printf("please input purpose and key:");
        int *key = (int*)malloc(sizeof(int)); 

        scanf("%d %d", &purpose,key);
        if (purpose == 0)
        {
            rbtree_insert_node(&rbtree,key,NIL);
        }
        else
        {
            RBTreeNode *n = rbtree_search(rbtree,key);
            if (n == NIL)
            {
                printf("no exist\n");
            }
            rbtree_delete_node(&rbtree,n);
        }
        
        recursion_traverse(rbtree);

        printf("\n");
    }
    
    return 0;
}